﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace SPVisualDev.Services
{
    public class Features
    {
        public static void InstallFeature(string FeatureName, bool Force)
        {
            SPFeatureDefinition feature_def = SPFarm.Local.FeatureDefinitions.Add(FeatureName + "\\feature.xml", Guid.Empty, Force);
        }

        public static void UninstallFeature(string FeatureName, bool Force)
        {
            SPFarm.Local.FeatureDefinitions.Remove(FeatureName + "\\feature.xml", Force);
        }

        public static void ActivateFeature(string FeatureName, string Url, bool Force)
        {
            ActivateDeactivateFeature(true, FeatureName, Url, Force);
        }

        public static void DeactivateFeature(string FeatureName, string Url, bool Force)
        {
            ActivateDeactivateFeature(false, FeatureName, Url, Force);
        }

        public static void PutFile(byte[] File, string DestinationPath)
        {
            throw new NotImplementedException();
            /*
            if (System.IO.File.Exists(DestinationPath))
            {
                System.IO.File.Delete(DestinationPath);
            }

            using (StreamWriter writer = new StreamWriter(DestinationPath))
            {
                writer.BaseStream.Write(File, 0, File.Length);
            }*/
        }

        private static void ActivateDeactivateFeature(bool Activate, string FeatureName, string Url, bool Force)
        {

            SPFeatureDefinition feature_def = SPFarm.Local.FeatureDefinitions[FeatureName];
            if (feature_def == null)
                throw new Exception("Feature " + FeatureName + " not installed on farm.");

            if (feature_def.Scope == SPFeatureScope.Farm)
            {
                SPWebService websvc = SPFarm.Local.Servers.GetValue<SPWebService>(string.Empty);
                if (Activate)
                {
                    websvc.Features.Add(feature_def.Id, Force);
                }
                else
                {
                    websvc.Features.Remove(feature_def.Id, Force);
                }
            }
            else if (feature_def.Scope == SPFeatureScope.Site)
            {
                using (SPSite site = new SPSite(Url))
                {
                    if (Activate)
                    {
                        site.Features.Add(feature_def.Id, Force);
                    }
                    else
                    {
                        site.Features.Remove(feature_def.Id, Force);
                    }
                }
            }
            else if (feature_def.Scope == SPFeatureScope.Web)
            {
                using (SPSite site = new SPSite(Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (Activate)
                        {

                            web.Features.Add(feature_def.Id, Force);
                        }
                        else
                        {
                            web.Features.Remove(feature_def.Id, Force);
                        }
                    }
                }
            }
            else if (feature_def.Scope == SPFeatureScope.WebApplication)
            {
                SPWebApplication web_app = SPWebApplication.Lookup(new Uri(Url));

                if (Activate)
                {
                    web_app.Features.Add(feature_def.Id, Force);
                }
                else
                {
                    web_app.Features.Remove(feature_def.Id, Force);
                }
            }
        }

        private static string ExecuteCommand(string ExecutablePath, string arguments)
        {
            using (System.Diagnostics.Process proc = new Process())
            {
                proc.StartInfo.FileName = ExecutablePath;
                proc.StartInfo.Arguments = arguments;

                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;

               
                proc.Start();

                string output;
                output = proc.StandardOutput.ReadToEnd();

                if (proc.ExitCode != 0)
                {
                    throw new Exception("Error occured. " + output + ". Exit code " + proc.ExitCode.ToString());
                }

                return output;
            }
        }


    }
}
